package com.flyox.game.fivetiger.tools;

import com.flyox.game.fivetiger.bean.CONSTS;
import com.flyox.game.fivetiger.bean.Position;

public class UtilBean {
	public int OFF = 80;
	public int DESK_WIDTH = 100;
	public int CHESS_WIDTH = 40;

	/**
	 * 得到左上角xy偏移量
	 * 
	 * @param i
	 * @return
	 */
	public int getOffset(int i) {
		return OFF + i * DESK_WIDTH;
	}

	/**
	 * 判断鼠标点击时，是否选中棋子
	 * 
	 * @param x
	 * @param y
	 * @return false ignore
	 */
	public Position posToRowCol(int x, int y) {
		if (x < 80 || y < 80 || x > 520 || y > 520) {
			return null;
		}
		int row = -1, col = -1;
		if (x > 80 && x < 120) {
			col = 0;
		} else if (x > 180 && x < 220) {
			col = 1;
		} else if (x > 280 && x < 320) {
			col = 2;
		} else if (x > 380 && x < 420) {
			col = 3;
		} else if (x > 480 && x < 520) {
			col = 4;
		}

		if (y > 80 && y < 120) {
			row = 0;
		} else if (y > 180 && y < 220) {
			row = 1;
		} else if (y > 280 && y < 320) {
			row = 2;
		} else if (y > 380 && y < 420) {
			row = 3;
		} else if (y > 480 && y < 520) {
			row = 4;
		}
		if (col != -1 && row != -1) {
			return new Position(row, col);
		} else {
			return null;
		}
	}

	/**
	 * 得到状态信息
	 * 
	 * @param isCurrentPlayer
	 * @param currentState   
	 * @return public static final int CMD_REQUEST_UNDO=12;//请求悔棋 public static
	 *         final int CMD_REFUSED_UNDO=13;//拒绝悔棋 public static final int
	 *         CMD_UNDO=14;//悔棋
	 */
	public String getStateMsg(boolean isCurrentPlayer, int currentState) {
		String msg = "结束";
		if (isCurrentPlayer) {
			if (currentState == CONSTS.CMD_ADD) {
				msg = "请下棋。。。";
			} else if (currentState == CONSTS.CMD_COVER) {
				msg = "请压棋。。。";
			} else if (currentState == CONSTS.CMD_MOVE) {
				msg = "请走棋。。。";
			} else if (currentState == CONSTS.CMD_CUT) {
				msg = "请拔棋。。。";
			} else if (currentState == CONSTS.CMD_REQUEST_UNDO) {
				msg = "是否同意悔棋。。。";
			}else if (currentState == CONSTS.CMD_SHOWOPT) {
				msg = "请确认。。。";
			}
		} else {
			if (currentState == CONSTS.CMD_ADD) {
				msg = "等待对方下棋。。。";
			} else if (currentState == CONSTS.CMD_COVER) {
				msg = "等待对方压棋。。。";
			} else if (currentState == CONSTS.CMD_MOVE) {
				msg = "等待对方走棋。。。";
			} else if (currentState == CONSTS.CMD_CUT) {
				msg = "等待对方拔棋。。。";
			} else if (currentState == CONSTS.CMD_REQUEST_UNDO) {
				msg = "请求悔棋，等待对方回答。。。";
			} else if (currentState == CONSTS.CMD_WAIT) {
				msg = "等待玩家加入。。。";
			}else if (currentState == CONSTS.CMD_SHOWOPT) {
				msg = "等待对方确认。。。";
			}
		}
		return msg;
	}

	public boolean chessCanMoveHereToHere(Position from, Position to) {
		if (from.row == to.row && Math.abs(to.col - from.col) == 1) {
			return true;
		} else if (from.col == to.col && Math.abs(to.row - from.row) == 1) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检查着一个棋子是否可以移动
	 * 
	 * @param layout
	 * @param pos
	 * @return
	 */
	public boolean chessCanMove(final int layout[][], Position pos) {
		if (pos.row - 1 >= 0 && layout[pos.row - 1][pos.col] == CONSTS.CHESS_EMPTY) {
			return true;
		} else if (pos.col + 1 <= 4 && layout[pos.row][pos.col + 1] == CONSTS.CHESS_EMPTY) {
			return true;
		} else if (pos.row + 1 <= 4 && layout[pos.row + 1][pos.col] == CONSTS.CHESS_EMPTY) {
			return true;
		} else if (pos.col - 1 >= 0 && layout[pos.row][pos.col - 1] == CONSTS.CHESS_EMPTY) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 检查是否有棋可以走
	 * 
	 * @param layout
	 * @param pos
	 * @return
	 */
	public boolean haveAnyChessCanMove(final int layout[][], int teamFlag) {
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				if (layout[i][j] == teamFlag) {
					if (i - 1 >= 0 && layout[i - 1][j] == CONSTS.CHESS_EMPTY) {
						return true;
					} else if (j + 1 <= 4 && layout[i][j + 1] == CONSTS.CHESS_EMPTY) {
						return true;
					} else if (i + 1 <= 4 && layout[i + 1][j] == CONSTS.CHESS_EMPTY) {
						return true;
					} else if (j - 1 >= 0 && layout[i][j - 1] == CONSTS.CHESS_EMPTY) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 检查是否有棋可以拔掉或者覆盖 成型的不可以拔
	 * 
	 * @param layout
	 * @param pos
	 * @return
	 */
	public boolean haveAnyChessRemove(final int layout[][], int teamFlag) {
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				if (layout[i][j] == teamFlag && checkShapeRemove(layout, new Position(i, j)) == 0) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 图形检查 检查是否形成 ，并返回可以拔掉棋子的个数
	 * 
	 * @param layout
	 * @param pos
	 * @return
	 */
	public int checkShapeRemove(final int layout[][], Position pos) {
		if (isTongTianGun(layout, pos)) {
			return 3;
		} else if (pos.row == 2 && isRowEqual(layout, pos)) {
			return 3;
		} else if (pos.col == 2 && isColEqual(layout, pos)) {
			return 3;
		} else if ((pos.row == 1 || pos.row == 3) && isRowEqual(layout, pos)) {
			return 2;
		} else if ((pos.col == 1 || pos.col == 3) && isColEqual(layout, pos)) {
			return 2;
		} else if (is4xie(layout, pos)) {
			return 2;
		} else if ((pos.row == 0 || pos.row == 4) && isRowEqual(layout, pos)) {
			return 1;
		} else if ((pos.col == 0 || pos.col == 4) && isColEqual(layout, pos)) {
			return 1;
		} else if (is3xie(layout, pos)) {
			return 1;
		} else if (isFang(layout, pos)) {
			return 1;
		} else {
			return 0;
		}
	}

	/**
	 * 检查整行是否相等 一虎 二虎 三虎
	 * 
	 * @param layout
	 * @param pos
	 * @return
	 */
	private boolean isRowEqual(final int layout[][], Position pos) {
		for (int i = 0; i < 5; i++) {
			if (layout[pos.row][i] != layout[pos.row][pos.col]) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 检查整列是否相等 一虎 二虎 三虎
	 * 
	 * @param layout
	 * @param pos
	 * @return
	 */
	private boolean isColEqual(final int layout[][], Position pos) {
		for (int i = 0; i < 5; i++) {
			if (layout[i][pos.col] != layout[pos.row][pos.col]) {
				return false;
			}
		}
		return true;
	}

	private boolean isTongTianGun(final int layout[][], Position pos) {
		int flag = layout[pos.row][pos.col];
		if (pos.row == pos.col && flag == layout[0][0] && flag == layout[1][1] && flag == layout[2][2] && flag == layout[3][3] && flag == layout[4][4]) {
			return true;
		} else if (pos.row + pos.col == 4 && flag == layout[0][4] && flag == layout[1][3] && flag == layout[2][2] && flag == layout[3][1] && flag == layout[4][0]) {
			return true;
		} else {
			return false;
		}
	}

	private boolean is4xie(final int layout[][], Position pos) {
		int flag = layout[pos.row][pos.col];

		Position[] poses = { new Position(0, 1), new Position(1, 2), new Position(2, 3), new Position(3, 4) };
		if (isPosIn(poses, pos) && (flag == layout[0][1]) && (flag == layout[1][2]) && (flag == layout[2][3]) && (flag == layout[3][4])) {
			return true;
		}

		poses[0] = new Position(1, 0);
		poses[1] = new Position(2, 1);
		poses[2] = new Position(3, 2);
		poses[3] = new Position(4, 3);
		if (isPosIn(poses, pos) && (flag == layout[1][0]) && (flag == layout[2][1]) && (flag == layout[3][2]) && (flag == layout[4][3])) {
			return true;
		}

		poses[0] = new Position(3, 0);
		poses[1] = new Position(2, 1);
		poses[2] = new Position(1, 2);
		poses[3] = new Position(0, 3);
		if (isPosIn(poses, pos) && (flag == layout[3][0]) && (flag == layout[2][1]) && (flag == layout[1][2]) && (flag == layout[0][3])) {
			return true;
		}

		poses[0] = new Position(4, 1);
		poses[1] = new Position(3, 2);
		poses[2] = new Position(2, 3);
		poses[3] = new Position(1, 4);
		if (isPosIn(poses, pos) && (flag == layout[4][1]) && (flag == layout[3][2]) && (flag == layout[2][3]) && (flag == layout[1][4])) {
			return true;
		}
		return false;
	}

	private boolean is3xie(final int layout[][], Position pos) {
		int flag = layout[pos.row][pos.col];

		Position[] poses = { new Position(2, 0), new Position(1, 1), new Position(0, 2) };
		// 2 只能检查跟 当前点有关的
		if (isPosIn(poses, pos) && (flag == layout[2][0]) && (flag == layout[1][1]) && (flag == layout[0][2])) {
			return true;
		}

		poses[0] = new Position(2, 0);
		poses[1] = new Position(3, 1);
		poses[2] = new Position(4, 2);
		if (isPosIn(poses, pos) && (flag == layout[2][0]) && (flag == layout[3][1]) && (flag == layout[4][2])) {
			return true;
		}

		poses[0] = new Position(0, 2);
		poses[1] = new Position(1, 3);
		poses[2] = new Position(2, 4);
		if (isPosIn(poses, pos) && (flag == layout[0][2]) && (flag == layout[1][3]) && (flag == layout[2][4])) {
			return true;
		}

		poses[0] = new Position(4, 2);
		poses[1] = new Position(3, 3);
		poses[2] = new Position(2, 4);
		if (isPosIn(poses, pos) && (flag == layout[4][2]) && (flag == layout[3][3]) && (flag == layout[2][4])) {
			return true;
		}

		return false;
	}

	private boolean isFang(final int layout[][], Position pos) {
		int flag = layout[pos.row][pos.col];
		// check 1 象限
		if (pos.row > 0 && pos.col < 4) {
			if (flag == layout[pos.row - 1][pos.col] && flag == layout[pos.row - 1][pos.col + 1] && flag == layout[pos.row][pos.col + 1]) {
				return true;
			}
		}
		// check 2 象限
		if (pos.row < 4 && pos.col < 4) {
			if (flag == layout[pos.row][pos.col + 1] && flag == layout[pos.row + 1][pos.col + 1] && flag == layout[pos.row + 1][pos.col]) {
				return true;
			}
		}
		// check 3 象限
		if (pos.row < 4 && pos.col > 0) {
			if (flag == layout[pos.row + 1][pos.col] && flag == layout[pos.row + 1][pos.col - 1] && flag == layout[pos.row][pos.col - 1]) {
				return true;
			}
		}
		// check 4 象限
		if (pos.row > 0 && pos.col > 0) {
			if (flag == layout[pos.row][pos.col - 1] && flag == layout[pos.row - 1][pos.col - 1] && flag == layout[pos.row - 1][pos.col]) {
				return true;
			}
		}
		return false;
	}

	private boolean isPosIn(Position[] poses, Position p) {
		for (int i = 0; i < poses.length; i++) {
			if (poses[i].equals(p)) {
				return true;
			}
		}
		return false;
	}
}
